//
//  ViewController.m
//  iosios
//
//  Created by 王成龙 on 16/4/24.
//  Copyright © 2016年 王成龙. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"
@interface ViewController (){
   // NSArray*aabb;
    NSMutableArray*aabb;

}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
  /*  aabb=@[
    @"    球球大作战    ",
    @"    大鱼吃小鱼    ",
    @"    王者荣耀      ",
    @"    部落冲突      ",
    @"    节奏大师      "
    ];*/
    self.title=@"将乐琦电话簿";
    UITableView*tableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableview.dataSource=self;
    [self.view addSubview:tableview];//1
    
    
    
  /*  NSBundle*bundle=[NSBundle mainBundle];
    NSURL*url=[bundle URLForResource:@"app" withExtension:@""];
    NSString*text=[[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"读取数据：\n%@",text);
    NSArray *arr = [text componentsSeparatedByString:@"\n"];
    NSMutableArray *app = [NSMutableArray new];
    for (int i = 0; i < arr.count; i++) {
        NSString *item = arr[i];
        NSArray *p = [item componentsSeparatedByString:@","];
        Person *arr = [Person new];
        arr.haoma = p[0];
        arr.name = p[1];
        [app addObject:arr];
    }
    aabb = app;*///2
    
    
   /* NSBundle*bundle=[NSBundle mainBundle];
    NSURL*url=[bundle URLForResource:@"tel" withExtension:@""];
    NSString*text=[[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"读取数据：\n%@",text);
    NSArray *ary = [text componentsSeparatedByString:@"\n"];
    NSMutableArray *tel = [NSMutableArray new];
    for (int i = 0; i < ary.count; i++) {
        NSString *item = ary[i];
        NSArray *p = [item componentsSeparatedByString:@","];
        Person *ary = [Person new];
        //Person *ary=[[Person alloc]init];
        ary.haoma = p[0];
        ary.name = p[1];
        [tel addObject:ary];
    }
    aabb=tel;
*///3
    
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *url = [bundle URLForResource:@"00001" withExtension:@"vcf"];
    NSString *text = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"读取数据：%@", text);
    
    NSArray *arr = [text componentsSeparatedByString:@"\n"];
    Person*item;
    NSMutableArray *tmp = [NSMutableArray new];
    for(NSString*p in arr){
        if([p hasPrefix:@"BEGIN"])
        {
            item=[Person new];
        }else if([p hasPrefix:@"END"])
        {
            [tmp addObject:item];
        }else if([p hasPrefix:@"TEL"])
        {
            
            NSArray *components=[p componentsSeparatedByString:@":"];
            NSString*app=components[1];
            item.haoma=app;
        }
        else if([p hasPrefix:@"FN:"]){
            NSArray *component=[p componentsSeparatedByString:@":"];
            NSString*abb=component[1];
            item.name=abb;
        }
        aabb=tmp;
    }

    
}

    
    

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return aabb.count;//1
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/*UITableViewCell*call=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
    reuseIdentifier:nil];
    call.textLabel.text=aabb[indexPath.row];*/ //1
    
   /* UITableViewCell*call=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1
    reuseIdentifier:nil];
    Person*x=aabb[indexPath.row];
    call.textLabel.text=x.name;
    call.detailTextLabel.text=x.haoma;*///2
    
    
  /*  UITableViewCell*call=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1
    reuseIdentifier:nil];
    Person*x=aabb[indexPath.row];
    //int row=[indexPath row];
   // Person*x=aabb [row];
    
    call.detailTextLabel.text=x.haoma;
    call.textLabel.text=x.name;
 *///3
    UITableViewCell*call=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    Person *p = aabb[indexPath.row];
    call.textLabel.text = p.name;
    call.detailTextLabel.text = p.haoma;
    return call;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
